#coding:utf-8

'''
author:bamboohat
'''

import pymysql.cursors
# import sys
# sys.path.append("W:\gitbrother\python\utils")
import configutil

class DbUtil:

    def getConn(self):
        cu = configutil.ConfigUtil
        host = cu.getMySqlConfig("jdbc.ip")
        port = cu.getMySqlConfig("jdbc.port")
        db = cu.getMySqlConfig("jdbc.db")
        user = cu.getMySqlConfig("jdbc.user")
        password = cu.getMySqlConfig("jdbc.password")
        conn = pymysql.connect(host=host, port=int(port), user=user, passwd=password, db=db, charset='utf8')
        return conn

    def close(self, cursor, conn):
        cursor.close()
        conn.commit()
        conn.close()

    def query(self, sql):
        conn = self.getConn()
        cursor = conn.cursor()
        cursor.execute(sql)
        result = cursor.fetchone()
        self.close(cursor, conn)
        return result

    def insert(self, sql):
        conn = self.getConn()
        cursor = conn.cursor()
        cursor.execute(sql)
        self.close(cursor, conn)

    def delete(self, sql):
        conn = self.getConn()
        cursor = conn.cursor()
        cursor.execute(sql)
        self.close(cursor, conn)

if __name__ == '__main__':
    dbutil = DbUtil()
    querySql = "select * from t_user"
    result = dbutil.query(querySql)
    print(result)
    insertSql = "INSERT INTO t_user (uuid, username, name, password, status, sex, tel, created_time) " \
                " VALUES (\"11001\", \"斗笠\", \"heihei\", \"123456\", 2, 1, null, now()) "
    print(insertSql)
    dbutil.insert(insertSql)
    deleteSql = "DELETE FROM t_user WHERE id = 2"
    dbutil.delete(deleteSql)